﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data; 

namespace StartStop.Helpers
{
   public  class TaskDespatcher
    {

        public const string LiveConnectionString = "data source=v2.startstop.me;initial catalog=startstop;persist security info=True;user id=sa;password=PASSWORD;";

        public static StartStop.ServiceResources.MessageResponse AddTask(StartStop.ServiceResources.Tasks oTask)
        {

            ServiceResources.MessageResponse oResponse = new ServiceResources.MessageResponse();
            SqlConnection oConn = new SqlConnection(LiveConnectionString);
            oConn.Open();

            SqlCommand oCmd = new SqlCommand(@"Insert into TaskDespatcher 
                                                            (TaskStatus, OverviewID, DataToRun, CreatedOn, OtherType) values
                                                            (@TaskStatus, @OverviewID, @DataToRun, @CreatedOn, @OtherType); select @@IDENTITY;

                                                ", oConn);

            // This is an add so the default is one which means it's new
            oCmd.Parameters.AddWithValue("@TaskStatus", 1);
            if (oTask.OverviewID.ToString() != "")
            {
                oCmd.Parameters.AddWithValue("@OVerviewID", -1);
            }
            else
            {
                oCmd.Parameters.AddWithValue("@OVerviewID", oTask.OverviewID);
            }


            if (oTask.DataToRun != null)
            {
                oCmd.Parameters.AddWithValue("@DataToRun", oTask.DataToRun);
            }
            else
            {
                oCmd.Parameters.AddWithValue("@DataToRun", System.DBNull.Value); 
            }

            oCmd.Parameters.AddWithValue("@CreatedOn", DateTime.Now);

            if (oTask.OtherType != null)
            {
                oCmd.Parameters.AddWithValue("@OtherType", oTask.OtherType);
            }
            else
            {
                oCmd.Parameters.AddWithValue("@OtherType", System.DBNull.Value); 
            }

            string _result = oCmd.ExecuteScalar().ToString();

            if (_result != "")
            {

                oResponse.ReturnID = Int64.Parse(_result);
                oResponse.Success = true; 
            }

            oConn.Close(); 

            return oResponse; 

        }

        public static StartStop.ServiceResources.Tasks GetTask(long OverviewID, string OtherType)
        {
            ServiceResources.Tasks oReturn = new ServiceResources.Tasks();
            SqlConnection oConn = new SqlConnection(LiveConnectionString);
            oConn.Open();
            string _command = "";

            if (OverviewID != -1)
            {
                _command = "select top 1 * from TaskDespatcher where OverviewID = " + OverviewID.ToString() + " and TaskStatus = 1"; 

            }
            else
            {
                _command = "select top 1 * from TaskDespatcher where OtherType = '" + OtherType + "' and TaskStatus = 1"; 
            }

            DataTable oTable = Sql.ReturnDataTable(_command);
            if (oTable.Rows.Count > 0)
            {
                oReturn.CreatedOn = DateTime.Parse(oTable.Rows[0]["Createdon"].ToString());
                oReturn.DataToRun = oTable.Rows[0]["DataToRun"].ToString(); 
                //oReturn.FinishedExecutionOn = 
                //oReturn.ModifiedOn = 
                oReturn.OtherType = oTable.Rows[0]["OtherType"].ToString();
                
                oReturn.OverviewID = long.Parse(oTable.Rows[0]["OverviewID"].ToString());
                oReturn.TaskID = (Guid)oTable.Rows[0]["TaskID"];
                oReturn.TaskStatus = int.Parse(oTable.Rows[0]["TaskStatus"].ToString()); 
                
                // Update the task as in processing, tasks need to be closed explitity. 
                SqlCommand oCmd = new SqlCommand(" update TaskDespatcher set TaskStatus = 2, ModifiedOn = GetDate() where TaskID = '" + oReturn.TaskID.ToString() + "'", oConn);
                oCmd.ExecuteNonQuery(); 



            }
            else
            {
                oReturn = null; 
            }

            oConn.Close(); 


            return oReturn; 
        }

        public static ServiceResources.MessageResponse CloseTask(Guid TaskID)
        {

            ServiceResources.MessageResponse oResponse = new ServiceResources.MessageResponse();
            oResponse.Success = true; 
            SqlConnection oConn = new SqlConnection(LiveConnectionString);
            oConn.Open();

            SqlCommand oCmd = new SqlCommand(" update TaskDespatcher set TaskStatus = 3, FinishedExecutionOn = getdate() where TaskID = '" + TaskID.ToString() + "'", oConn);
            oCmd.ExecuteNonQuery(); 

            oConn.Close(); 


            return oResponse; 

        }

    }
}
